var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

Page({
  data: {
    orderList: [],
    status: '',
    clientCreateBy: '',
    startNum: 1,
    hasMore: true
  },
  onLoad: function (options) {
    console.log("传过来的：" + options.status);
    console.log("用户id" + (wx.getStorageSync('userInfo')).id);
    // 页面初始化 options为页面跳转所带来的参数

    this.setData({
      status: options.status ? options.status:'',
      clientCreateBy: (wx.getStorageSync('userInfo')).id
    });
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getOrderList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  getOrderList() {
    let that = this;
    let status = that.data.status;
    if (status == 0){
      status = '';
    }else{
      status = status - 1;
    }
    that.setData({
      startNum: 1,
      hasMore: true
    })
    var pageNum = that.data.startNum
    util.request(api.OrderDeviceList, {
      clientCreateBy: that.data.clientCreateBy,
      status: status,
      current: pageNum,
      size: 10,
    },"POST").then(function (res) {
      var l = res.records
      var total = l.length
      if (total < 9) {
        var hasMore = false
      } else {
        var hasMore = true
      }

      that.setData({
        orderList: res.records,
        startNum: 2,
        hasMore: hasMore
      });
    });
  },


  //上拉获取更多列表
  hotActivityLoadMore: function (e) {
    var that = this
    that.listPage()
  },

  //分页查询列表
  listPage: function () {
    var that = this;
    //判断是否还有下一页
    if (that.data.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })
      var pageNum = that.data.startNum
      let status = that.data.status
      console.log(status);

      util.request(api.OrderList, {
        userId: that.data.userId,
        status: '',
        current: pageNum,
        size: 10,
      }).then(function (res) {
        var l = res.records
        var total = JSON.parse(l.length)
        if (total < 9) {
          var hasMore = false
        } else {
          var hasMore = true
        }
        pageNum++
        var lis = that.data.orderList
        lis = lis.concat(res.records)
        that.setData({
          orderList: lis,
          startNum: pageNum,
          hasMore: hasMore
        });
        wx.hideLoading();
      }).catch(function (res) {
        wx.hideLoading();
        console.log("获取数据失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },

  switchTab: function (event) {
    var that = this
    let status = event.currentTarget.dataset.index;
    that.setData({
      status: status
    });
    that.getOrderList();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    this.getOrderList();
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  selectDetail: function (event) {
    //跳转到查看订单详情页面
    let i = event.currentTarget.dataset.index;
    let query = this.data.orderList[i];
    query = JSON.stringify(query);
    console.log(query);
    wx.navigateTo({
      url: '../deviceOrderDetail/deviceOrderDetail?query=' + query
    });
  },
  // “去付款”按钮点击效果
  payOrder: function () {
    let that = this;
    util.request(api.OrderPrepay, {
      'openid': "获取用户的openid", //需要你获取用户的openid
      'title': "shoplist", //订单名称 这里随便定义shoplist
      'price': 0.1 * 100, //一毛钱
      'goodsid': 100, ///商品ID 这里随便定义成100
    }, 'POST').then(function (res) {
      if (res.errno === 0) {
        const payParam = res.data;
        console.log("支付过程开始");
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.packageValue,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log("支付过程成功");
            // 订单付款状态修改
            that.OrderInfopay();
          },
          'fail': function (res) {
            console.log("支付过程失败");
            util.showErrorToast('支付失败');
          },
          'complete': function (res) {
            console.log("支付过程结束")
          }
        });
      } else {
        util.showErrorToast('订单状态已变更，请刷新后再进行操作');
      }
    });
  },
  // 订单付款状态修改
  OrderInfopay: function () {
    util.request(api.OrderPay, {
      orderNo: '订单编号',
      status: '订单状态',
    }).then(function (res) {
      util.redirect('/pages/ucenter/order/order');
    });

  }
})